clear all
set more off, permanently

global replica_dir="<add path to your working directory>"
global data_dir="$replica_dir/data"
global output_dir="$replica_dir/output"

global simce_data_dir="<add path to your SIMCE data directory>"
*------------------------------------------------------------------------------*

* SIMCE 2013

*------------------------------------------------------------------------------*

**# Student scores

/*
	Note:
	
	idalumno: 	unique identifier for "mrun-letra_curso-cod_curso" record  
				important when linking it to teacher survey piece, use together with letra_curso-cod_curso
	
	mrun: unique student identifier to link data across years
	
------------------------------------------------------------------------------*/

cd "$data_dir" 

use "$simce_data_dir/8B/simce8b2013/Archivos DTA (Stata)/simce8b2013_alu_publica_final.dta", clear
drop if rbd==.

keep if noptje_lect8b_alu==0 | noptje_mate8b_alu==0 | noptje_nat8b_alu==0 // con puntaje

duplicates report mrun
duplicates examples mrun
duplicates tag mrun, g(ndupobs)
//browse if ndupobs>0
unique mrun

keep if ndupobs==0

rename  (ptje_lect8b_alu 	ptje_mate8b_alu 	ptje_nat8b_alu ) /// 
		(lect_post			mate_post			nat_post)


keep idalumno mrun rbd lect_post mate_post nat_post    // noptje_lect8b_alu noptje_mate8b_alu noptje_nat8b_alu

save "$data_dir/simce_b8_2013.dta", replace




*------------------------------------------------------------------------------*

**#  School survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/8B/simce8b2013/Archivos DTA (Stata)/simce8b2013_rbd_publica_final.dta", clear

egen sch_stno_post = rowmax(nalu_8b_rbd nalu_lect8b_rbd nalu_mate8b_rbd nalu_nat8b_rbd)

rename  (cod_depe1 		cod_reg_rbd_2013) /// 
		(sch_depe1_post	cod_reg_rbd_post)

gen 	sch_gse_post=1 if cod_grupo=="1" // SES group: Alto--Bajo
replace sch_gse_post=2 if cod_grupo=="2" // SES group: Alto--Bajo
replace sch_gse_post=3 if cod_grupo=="3" // SES group: Alto--Bajo
replace sch_gse_post=4 if cod_grupo=="4" // SES group: Alto--Bajo
replace sch_gse_post=5 if cod_grupo=="5" // SES group: Alto--Bajo
replace sch_gse_post=. if cod_grupo=="." // SES group: Alto--Bajo
replace sch_gse_post=. if cod_grupo=="NULL" // SES group: Alto--Bajo

keep  rbd sch_stno_post sch_depe1_post sch_gse_post cod_reg_rbd_post

merge 1:m rbd using "$data_dir/simce_b8_2013.dta"

//browse if _merge==1
keep if _merge==3
drop _merge

sort mrun

rename rbd rbd_post

save "$data_dir/simce_b8_2013.dta", replace


*------------------------------------------------------------------------------*

